#include<bits/stdc++.h>
using namespace std;
struct node
{
    int data;
    struct node* l, * r;
};
int tt = 0;
struct node* getroot(vector<int>pre, vector<int>in, int start_pre, int last_pre, int start_in, int last_in);
vector<vector<int>>bfs(node* root);
int main(void)
{
    int n;
    cin >> n;
    vector<int>pre(n);
    vector<int>in(n);
    for (int i = 0; i < n; i++)
        cin >> pre[i];
    for (int i = 0; i < n; i++)
        cin >> in[i];
    struct node* root = getroot(pre, in, 0, n - 1, 0, n - 1);
    vector<vector<int>>result=bfs(root);
    int flag = 0;
    for (int i = 0; i < result.size(); i++)
    {
        for (int j = 0; j < result[i].size(); j++)
        {

            cout << result[i][j]<<' ';
        }
    }

}
struct node* getroot(vector<int>pre, vector<int>in, int start_pre, int last_pre, int start_in, int last_in)
{
    int root = pre[start_pre];
    int index = 0;
    struct node* t = new struct node;
    t->data = root;
    for (int i = start_in; i <= last_in; i++)
    {
        if (in[i] == root)
        {
            index = i;
            break;
        }

    }
    if (index == start_in)
    {
        t->l = NULL;
    }
    else
    {
        t->l = getroot(pre, in, start_pre + 1, start_pre + index - start_in, start_in, index - 1);
    }
    if (index == last_in)
    {
        t->r = NULL;
    }
    else
    {
        t->r = getroot(pre, in, start_pre + index - start_in + 1, last_pre, index + 1, last_in);
    }
    return t;
}
vector<vector<int>>bfs(node* root) {
    vector<vector<int>>result;
    deque<node*>d;
    if (root == NULL)
    {
        return result;
    }
    d.push_back(root);
    while (d.size() != 0)
    {
        int size = d.size();
        vector<int>arr;
        while (size--)
        {
            node* node = d[0];
            d.pop_front();
            arr.push_back(node->data);
            if (node->l)
            {
                d.push_back(node->l);
            }
            if (node->r)
            {
                d.push_back(node->r);
            }
        }
        result.push_back(arr);
    }
    return result;
}